package com.nebula.controller.salary;

import com.alibaba.fastjson.JSON;
import com.nebula.common.Const;
import com.nebula.common.DataGridFooter;
import com.nebula.common.PageResult;
import com.nebula.controller.BaseController;
import com.nebula.service.ISalaryService;
import com.nebula.service.impl.SalaryServiceImpl;
import com.nebula.vo.SalaryVo;
import com.nebula.vo.UserVo;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CcccX
 */
@WebServlet(name = "HistorySalaryController", urlPatterns = "/historySalary")
public class HistorySalaryController extends BaseController {

    private final ISalaryService salaryService = new SalaryServiceImpl();

    public String index(HttpServletRequest request, HttpServletResponse response) {
        String navId = request.getParameter("id");
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        if (salaryService.checkNavRole(user.getRoleId(), navId)) {
            return Const.FORWARD + Const.DEFAULT_URL + "salary/history-salary.jsp";
        }
        return Const.FORWARD + "role-error.jsp";
    }

    public String findByUserId(HttpServletRequest request, HttpServletResponse response) {
        UserVo user = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        String size = request.getParameter("rows");
        String page = request.getParameter("page");
        if (StringUtils.isBlank(size)) {
            size = Const.SIZE;
        }
        if (StringUtils.isBlank(page)) {
            page = Const.PAGE;
        }
        int year = Integer.parseInt(request.getParameter("year"));
        PageResult<SalaryVo> pageResult = salaryService.findByUserIdAndPage(user.getId(), Integer.parseInt(page),
                Integer.parseInt(size), true, year);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageResult.getTotalCount());
        map.put("rows", pageResult.getList());
        DataGridFooter dataGridFooter = salaryService.findTotalSalary(user.getId(), true, year);

        return getResult(pageResult, map, dataGridFooter);
    }

    static String getResult(PageResult<SalaryVo> pageResult, Map<String, Object> map, DataGridFooter dataGridFooter) {
        List<SalaryVo> salaryVos = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            SalaryVo salaryVo = new SalaryVo();
            if (i == 0) {
                salaryVo.setRemark("当前页合计：");
                salaryVo.setSalary(pageResult.getCurrTotalSalary());
            } else {
                salaryVo.setRemark("总计：");
                salaryVo.setSalary(dataGridFooter.getTotalSalary());
            }
            salaryVos.add(salaryVo);
        }

        map.put("footer", salaryVos);
        return Const.JSON + JSON.toJSONString(map);
    }
}
